$(function () {

    var loading = false;
    // 分页允许返回的最大条数，超过禁止访问后台
    var maxItems = 20;
    // 每页多少条
    var pageSize = 10;
    var pageNum = 1;
    var productCategoryId = '';
    var productName = '';
    var shopId = getQueryString('shopId');
    //获取本店铺信息以及商品类别信息列表的URL
    var searchDivUrl = '/o2o/frontend/listshopdetailpageinfo?shopId=' + shopId;
    //列出商品列表url
    var listUrl = '/o2o/frontend/listproductsbyshop';

    getSearchDivData();

    addItems(pageSize, pageNum);

    //给兑换礼品的a标签复制兑换礼品的url 2.0讲解
    $('#exchangelist').attr('href', '/o2o/frontend/awardlist?shopId=' + shopId);

    function getSearchDivData() {
        $.getJSON(searchDivUrl, function (data) {
            if (data.success) {
                var shop = data.shop;
                $('#shop-cover-pic').attr('src', shop.shopImg);
                $('#shop-update-time').html(new Date(shop.lastEditTime).Format('yyyy-MM-dd'));
                $('#shop-name').html(shop.shopName);
                $('#shop-desc').html(shop.shopDesc);
                $('#shop-addr').html(shop.shopAddr);
                $('#shop-phone').html(shop.phone);

                //获取后台返回该店铺的商品列表
                var productCategoryList = data.productCategoryList;
                var html = '';

                //遍历产品,生成可以点击搜索响应产品类别下的商品的a标签
                productCategoryList.map(function (item, index) {
                    html += '<a href="#" class="button" data-product-search-id='
                        + item.productCategoryId
                        + '>'
                        + item.productCategoryName
                        + '</a>';
                });
                $('#shopdetail-button-div').html(html);
            }
        });
    }


    function addItems(pageSize, pageIndex) {
        // 生成新条目的HTML
        var url = listUrl + '?'
            + 'pageIndex=' + pageIndex
            + '&pageSize=' + pageSize
            + '&productCategoryId=' + productCategoryId
            + '&productName=' + productName
            + '&shopId=' + shopId;
        loading = true;

        $.getJSON(url, function (data) {
            if (data.success) {
                maxItems = data.count;
                var html = '';
                data.productList.map(function (item, index) {
                    html += '' + '<div class="card" data-product-id='
                        + item.productId + '>'
                        + '<div class="card-header">' + item.productName
                        + '</div>' + '<div class="card-content">'
                        + '<div class="list-block media-list">' + '<ul>'
                        + '<li class="item-content">'
                        + '<div class="item-media">' + '<img src="'
                        + item.imgAddr + '" width="44">' + '</div>'
                        + '<div class="item-inner">'
                        + '<div class="item-subtitle">' + item.productDesc
                        + '</div>' + '</div>' + '</li>' + '</ul>'
                        + '</div>' + '</div>' + '<div class="card-footer">'
                        + '<p class="color-gray">'
                        + new Date(item.lastEditTime).Format("yyyy-MM-dd")
                        + '更新</p>' + '<span>点击查看</span>' + '</div>'
                        + '</div>';
                });
                $('.list-div').append(html);
                var total = $('.list-div .card').length;
                if (total >= maxItems) {
                    // 删除加载提示符
                    $('.infinite-scroll-preloader').hide();
                } else {
                    $('.infinite-scroll-preloader').show();
                }
                pageNum += 1;
                loading = false;
                $.refreshScroller();

            }
        });
    }

    /**
     * 下滑屏幕自动分页
     */
    $(document).on('infinite', '.infinite-scroll-bottom', function () {
        if (loading) {
            return;
        }
        addItems(pageSize, pageNum);
    });

    /**
     * 选择新的产品类别之后，重置页码，清空原先的商品列表，按照新的类别去查询
     */
    $('#shopdetail-button-div').on('click', '.button', function (e) {
        //获取商品类别Id
        productCategoryId = e.target.dataset.productSearchId;
        if (productCategoryId) {
            // 若之前一选定了别的category 则移除其选定的效果，改成选定新的
            if ($(e.target).hasClass('button-fill')) {
                $(e.target).removeClass('button-fill');
                productCategoryId = '';
            } else {
                $(e.target).addClass('button-fill').siblings().removeClass('button-fill');
            }
            $('.list-div').empty();
            pageNum = 1;
            addItems(pageSize, pageNum);
        }
    });

    /**
     * 点击商品的卡品进入该商品的详情页
     */
    $('.list-div').on('click', '.card', function (e) {
        var productId = e.currentTarget.dataset.productId;
        window.location.href = '/o2o/frontend/productdetail?productId=' + productId;
    });

    /**
     * 需要查询的名字发生变化，重置页码，清空原先的商品列表，按照新的名字进行查询
     */
    $('#search').on('change', function (e) {
        productName = e.target.value;
        $('.list-div').empty();
        pageNum = 1;
        addItems(pageSize, pageNum);
    });

    /**
     * 点击后打开右侧栏
     */
    $('#me').on(function () {
        $.openPanel('#panel-right-demo');
    });

    $.init();


})